package com.sofly.controller.group3;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.sofly.entity.DataGrid;
import com.sofly.entity.PurchaseList;
import com.sofly.service.group3.IPurchaseListService;
import com.sofly.service.group3.ISuppliersService;
/**
 * 订单控制类
* <p>Title:PurchaseListController </p>
* <p>Description: </p>
* <p>Company: </p> 
* @author ChengShiMin
* @date 下午5:52:11
 */
@Controller
public class PurchaseListController {

	@Autowired
	private DataGrid dataGrid;
	@Autowired
	private IPurchaseListService purchaseListService;
	@Autowired
	private AbstractPdfView abstractPdfView;
	@Autowired
	private ISuppliersService suppliersService;
	@RequestMapping("login1")
	public String Login(){
		return "Purchase";
	}
	/**
	 * 添加订单
	 * @param purchaseList
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/href/addPurchaseList")
	public int addPurchaseList(PurchaseList purchaseList){
		
		int i = purchaseListService.addPurchaseList(purchaseList);
		return i;
	}
	/**
	 * 生成并打印订单
	 * @return
	 */
	@RequestMapping(value = "/href/pdf")
	@ResponseBody
    public ModelAndView viewPdf(String purchaseId) {
        Map<String, Object> model = new HashMap<>();
        PurchaseList purchaseList = purchaseListService.getPurchase(purchaseId);
        purchaseList.setRemark("已下单");
        purchaseListService.editPurchaseList(purchaseList);
        model.put("purchaseList",purchaseListService.getPurchase(purchaseId));
        model.put("suppliers",suppliersService.getSuppliersBySupplierId(purchaseListService.getPurchase(purchaseId).getSupplierId()));
        return new ModelAndView(abstractPdfView, model);
    }
	/**
	 * 获取订单列表
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/href/purchaselist")
	@ResponseBody
	public DataGrid getPurchaseList(String page,String rows,String saveDate) throws UnsupportedEncodingException{		
		int page1 = Integer.parseInt(page);
		int rows1 = Integer.parseInt(rows);
		List<PurchaseList> list = new ArrayList<>();
		int total = 0;
		 if(saveDate==null||saveDate==""){
	     list = purchaseListService.getPurchaseList((page1-1)*rows1,rows1);
	     total = purchaseListService.getCount();
	     dataGrid.setRows(list);
	     dataGrid.setTotal(total);
		 }else{
			 list = purchaseListService.getPurchaseListByDate(saveDate, (page1-1)*rows1,rows1);
			 total = purchaseListService.getCount1(saveDate);
			 dataGrid.setRows(list);
			 dataGrid.setTotal(total);
		 }
	   return dataGrid;
	}
	@RequestMapping(value="/href/purchaselist1")
	@ResponseBody
	public DataGrid getgetPurchaseList1(String page,String rows,String minprice,String maxprice){
		int page1 = Integer.parseInt(page);
		int rows1 = Integer.parseInt(rows);		
		List<PurchaseList> list = new ArrayList<>();
		int total = 0;
		if(minprice==null||minprice==""||maxprice==null||maxprice==""){
			 list = purchaseListService.getPurchaseList((page1-1)*rows1,rows1);
		     total = purchaseListService.getCount();
		     dataGrid.setRows(list);
		     dataGrid.setTotal(total);
		}else{
			 double minprice1 = Double.parseDouble(minprice);
			 double maxprice2 = Double.parseDouble(maxprice);
			 list = purchaseListService.selectPurchaseListByPrice(minprice1,maxprice2,(page1-1)*rows1,rows1);
			 total = purchaseListService.getCount2(minprice1, maxprice2);
			 dataGrid.setRows(list);
		     dataGrid.setTotal(total);
		}
		return dataGrid;
	}
	@RequestMapping(value="/href/purchaselist2")
	@ResponseBody
	public DataGrid getgetPurchaseList2(String page,String rows,String starttime,String endtime){
		int page1 = Integer.parseInt(page);
		int rows1 = Integer.parseInt(rows);		
		List<PurchaseList> list = new ArrayList<>();
		int total = 0;
		if(starttime==null||starttime==""||endtime==null||endtime==""){
			 list = purchaseListService.getPurchaseList((page1-1)*rows1,rows1);
		     total = purchaseListService.getCount();
		     dataGrid.setRows(list);
		     dataGrid.setTotal(total);
		}else{
			 list = purchaseListService.getPurchaseListByDate1(starttime,endtime,(page1-1)*rows1,rows1);
			 total = purchaseListService.getCount3(starttime, endtime);
			 dataGrid.setRows(list);
			 dataGrid.setTotal(total);
		}
		return dataGrid;
	}
	/**
	 * 修改订单
	 * @param purchaseList
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/href/editPurchaseList")
	public int editPurchaseList(PurchaseList purchaseList){
		
		int i = purchaseListService.editPurchaseList(purchaseList);
		return i;
	}
	/**
	 * 查询订单
	 * @param purchaseId
	 * @return
	 */
	@RequestMapping(value="/href/SearchPurchaseList")
	@ResponseBody
	public PurchaseList SearchPurchaseList(String purchaseId){
		
		return purchaseListService.getPurchase(purchaseId);
		
	}
	/**
	 * 获取供货商编号列表
	 * @return
	 */
	@RequestMapping(value="/SupplierIdList")
	@ResponseBody
	public String getSupplierIdList(){
		List<String> list = suppliersService.getSupplierIdList();
		String string1="";
		for (int i = 1; i <list.size(); i++) {
			 string1 +="{'id':"+i+",'text':"+"'"+list.get(i)+"'},";
		}
		string1+="{'id':"+list.size()+",'text':"+"'"+list.get(list.size()-1)+"'}";
		string1 = "["+string1+"]";
		return  string1;
	}
	 
}
